﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GalaSoft.MvvmLight.Messaging;
using Bymed.SpineScan.WPF.Shared;
using Bymed.SpineScan.WPF.ViewModel;
using Bymed.SpineScan.WPF.ViewModel.TestItemViewModel;
using Bymed.SpineScan.DataModel;

namespace Bymed.SpineScan.WPF
{
    /// <summary>
    /// Interaction logic for PatientDataSearchPage.xaml
    /// </summary>
    public partial class PatientDataSearchPage : PageBase
    {
        public PatientDataSearchPage()
        {
            InitializeComponent();
            InitializePageUnloadStoryboard();
        }

        protected override void RegisterMessages()
        {
            base.RegisterMessages();

            RegisterDeleteConfirmMessage();
            RegisterRequestGoToNextMessage();
            RegisterTestSummaryReportMessage();
            RegisterShowSingleTestDetailMessage();
        }

        /// <summary>
        /// Register the DeleteDataConfirmMessage from PatientDataSearchViewModel.
        /// </summary>
        private void RegisterDeleteConfirmMessage()
        {
            Messenger.Default.Register<DialogMessage>(this, PatientDataSearchViewModel.DeleteDataConfirmMessageToken, dialog =>
            {
                var confirmResult = MessageBox.Show(dialog.Content, dialog.Caption, dialog.Button, dialog.Icon);
                dialog.ProcessCallback(confirmResult);
            });
        }

        /// <summary>
        /// Register the RequestGoToNextMessage from PatientDataSearchViewModel.
        /// </summary>
        private void RegisterRequestGoToNextMessage()
        {
            Messenger.Default.Register<PatientBasicInfoDisplayViewModel>(this, PatientDataSearchViewModel.GoToNextPageMessageToken, p =>
                {
                    var patientPage = new PatientBasicInfoPage();
                    var patientViewModel = (PatientBasicInfoViewModel)patientPage.DataContext;
                    patientViewModel.IsNewData = p.IsNewData;
                    patientViewModel.Number = p.Number;
                    patientViewModel.Name = p.Name;
                    patientViewModel.IsMale = p.IsMale;
                    patientViewModel.IsFemale = p.IsFemale;
                    patientViewModel.Birthday = p.Birthday;
                    patientViewModel.Height = p.Height;
                    patientViewModel.Weight = p.Weight;
                    patientViewModel.Remark = p.Remark;

                    GoToPage(patientPage, false);
                });
        }

        /// <summary>
        /// Register the ShowTestSummaryReportMessage from AllTestResultsViewModel.
        /// </summary>
        private void RegisterTestSummaryReportMessage()
        {
            Messenger.Default.Register<AllTestResultsViewModel>(this, AllTestResultsViewModel.ShowTestSummaryReportMessageToken, p =>
            {
                try
                {
                    var reportWindow = new TestReportWindow();
                    var testReportViewModel = reportWindow.DataContext as TestReportViewModel;
                    testReportViewModel.TestDateDisplay = p.TestDateDisplay;
                    testReportViewModel.PatientBasicInfoDisplayViewModel = ((PatientDataSearchViewModel)DataContext).SelectedPatient;
                    testReportViewModel.DignosticMessageList = new ObservableCollection<DiagnosticMessageReference>(p.DiagnosticMessageList);
                    testReportViewModel.AttachedDiagnosticMessage = p.AttachedDiagnosticMessage;
                    testReportViewModel.ResetProperties();
                    ViewModelHelper.CopyAllTestItemsViewModel(p, testReportViewModel);

                    reportWindow.ShowDialog();
                }
                catch (Exception ex)
                {
                    MyViewModelBase.RequestDisplayErrorMessage(ex.Message);
                }
            });
        }

        /// <summary>
        /// Register the ShowSingleTestDetailMessage from AllTestResultsViewModel.
        /// </summary>
        private void RegisterShowSingleTestDetailMessage()
        {
            Messenger.Default.Register<SingleTestItemViewModel>(this, AllTestResultsViewModel.ShowSingleTestDetailMessageToken, p =>
            {
                try
                {
                    var singleTestDetailWindow = new SingleTestDetailWindow();
                    var singleTestDetailViewModel = singleTestDetailWindow.DataContext as SingleTestDetailViewModel;
                    singleTestDetailViewModel.PatientBasicInfoDisplayViewModel = (DataContext as PatientDataSearchViewModel).SelectedPatient;
                    singleTestDetailViewModel.LoadHistoryData(p);
                    singleTestDetailWindow.ShowDialog();
                }
                catch (Exception ex)
                {
                    MyViewModelBase.RequestDisplayErrorMessage(ex.Message);
                }
            });
        }

        /// <summary>
        /// Generate DataGridColumn and setup binding based on current all CustomizedTestItems.
        /// </summary>
        private void GenerateDataGridColumnBasedOnAllCustomizedTestItems()
        {
            var viewModel = DataContext as PatientDataSearchViewModel;
            var index = 0;

            foreach (var item in viewModel.AllCustomizedTestItems)
            {
                var button = new FrameworkElementFactory(typeof(Button));
                button.SetResourceReference(Button.StyleProperty, "LinkButton");
                button.SetBinding(Button.ContentProperty, new Binding(string.Format("TestItemList[{0}].FormattedTestResult", index)));
                button.SetBinding(Button.CommandProperty, new Binding("ShowSingleTestDetailCommand"));
                button.SetBinding(Button.CommandParameterProperty, new Binding(string.Format("TestItemList[{0}]", index)));

                DataGridTestResults.Columns.Add(new DataGridTemplateColumn
                {
                    CellTemplate = new DataTemplate
                    {
                        VisualTree = button
                    },
                    Width = 150,
                    Header = item.ItemTitle
                });

                index++;
            }
        }

        private void CurrentPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                GenerateDataGridColumnBasedOnAllCustomizedTestItems();
            }
            catch (Exception ex)
            {
                MyViewModelBase.RequestDisplayErrorMessage(ex.Message);
            }
        }

        private void ButtonBack_Click(object sender, RoutedEventArgs e)
        {
            GoToPage(new PatientBasicInfoPage(), false);
        }
    }
}
